<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 显示添加的表单 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入搜索内容">
            <el-button
              slot="append"
              icon='el-icon-search'
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            @click="addGoods"
          >添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 显示数据的表格 -->
      <el-table
        stripe
        border
        :data="goodsList"
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
        ></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="95px"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="70px"
        ></el-table-column>
        <el-table-column
          label="创建时间"
          width="155px"
        >
          <template slot-scope="scope">
            {{ scope.row.upd_time |  dateFormat }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="185px"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
            >编辑</el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="removeById(scope.row.goods_id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能展示 -->
      <el-pagination
        @size-change="changeSize"
        @current-change="changeCurPage"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      goodsList: []
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      // 在使用axios发起请求的时候，只有get需要参数的情况，要加params
      let { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error('请求数据失败')
      console.log(res.data)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    changeSize(size) {
      this.queryInfo.pagesize = size
      this.getGoodsList()
    },
    changeCurPage(curPage) {
      this.queryInfo.pagenum = curPage
      this.getGoodsList()
    },
    async removeById(id) {
      // 1.0 处理提示的内容 confirm
      let resText = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (resText !== 'confirm') return this.$message.info('取消删除了')

      // 2.0  发起删除的请求，填入接收的id
      let { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除参数失败')
      }
      this.$message.success('删除参数成功')
      // 重新获取数据
      this.getGoodsList()
    },
    addGoods() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 30px;
}
</style>